(function (aopc) {
    aopc.addExpandPage(820, function (get) {
        const Common = get('UtilsCommon', 'Common');
        return {
            expandInit() {
                this.meeting_vote_view = this.getProperty('meeting_vote_view');
                this.item_view = this.getViewProperty(this.meeting_vote_view, 'item_view');

                this.container_view = this.getProperty('container_view');

                this.receiveFunctions.endVote = this.endVote.bind(this);
            },
            endVote(data) {
                if (data.MeetingId === this.pageData.MeetingId) {
                    this.replacePage('/meetingSubjectList.html?MeetingId=' + data.MeetingId);
                }
            },
            expandDataLoad({ data }) {
                if (data.MeetingId) {
                    this.entityData = data;
                    if (data.Votes.length > 0) {
                        this.setSelectVoteItem(0);
                        this.meeting_vote_view.setIsVisible(true);
                    }
                    else this.alert('暂无投票项！')
                }
            },
            reload(pageData) {
                this.pageData = pageData;
                this.container_view.reLoad();
            },
            setSelectVoteItem(index) {
                const [left_view, center_view, right_view] = this.meeting_vote_view.properties[2].properties;

                this.selectIndex = index;
                this.selectVote = this.entityData.Votes[index];

                this.meeting_vote_view.properties[0].properties.forEach(p => {
                    this.setPropertyValue(p, this.selectVote[p.name]);
                });

                this.setIsVisible(left_view.properties[0], this.selectIndex > 0);
                this.setIsVisible(right_view.properties[0], this.selectIndex < this.entityData.Votes.length - 1);

                const { SelectItems, VoteItems, IsChange, Status } = this.selectVote;

                const enable = Status === 1 && (IsChange || !SelectItems);
                this.selectVote.disabled = !enable;

                const [ok_button] = this.meeting_vote_view.properties[3].properties;
                this.setIsVisible(ok_button, enable);

                const items = VoteItems.replaceAll('；', ';').split(';');

                const selectList = SelectItems ? SelectItems.split(';') : [];

                const list = [];
                items.forEach(item => {
                    const isSelected = selectList.includes(item);
                    const p = Common.clone(this.item_view);
                    p.id = Common.createGuid();
                    p.style.background = isSelected ? 'rgba(255,255,255,0.9)' : 'rgba(255,255,255,0.5)';
                    const img_2 = this.getViewProperty(p, 'img_2');
                    img_2.isVisible = isSelected;
                    p.img_2 = img_2;
                    const item_text = this.getViewProperty(p, 'item_text');
                    item_text.text = item;
                    p.isSelected = isSelected;
                    p.selectItem = item;
                    p.eventActionName = 'setSelectItem'
                    list.push(p);
                });

                center_view.properties = list;
                center_view.getValue = () => {
                    const values = [];
                    for (let i = 0; i < center_view.properties.length; i++) {
                        const p = center_view.properties[i];
                        if (p.isSelected) {
                            values.push(p.selectItem);
                            if (this.selectVote.VoteType === 0) break;
                        }
                    }
                    return values.join(';');
                }

                center_view.refresh && center_view.refresh();

                this.selectItemProeprties = list;
            },
            setPropertySelected(property, selected) {
                property.isSelected = selected;
                property.style.background = selected ? 'rgba(255,255,255,0.9)' : 'rgba(255,255,255,0.5)';
                this.setIsVisible(property.img_2, selected);
                property.refresh();
            },
            setSelectItem({ property }) {
                const { VoteType, disabled } = this.selectVote;
                if (disabled) return;
                if (VoteType === 1) this.setPropertySelected(property, !property.isSelected);
                else {
                    this.selectItemProeprties.forEach(p => {
                        this.setPropertySelected(p, p === property);
                    });
                }
            },
            setPrevVote() {
                this.setSelectVoteItem(this.selectIndex - 1);
            },
            setNextVote() {
                this.setSelectVoteItem(this.selectIndex + 1);
            },
            setIsVisible(p, v) {
                p.isVisible = v;
                p.setIsVisible && p.setIsVisible(v);
            },
            setPropertyValue(p, v) {
                p.value = v;
                p.setValue && p.setValue(v);
            },
            expandSetVoteRecordData({ entityData }) {
                if (Common.isNullOrEmpty(entityData.VoteItems)) {
                    this.alert("请选择投票选项！")
                    return false;
                }
                entityData.VoteId = this.selectVote.VoteId;
                entityData.MeetingId = this.pageData.MeetingId;
                entityData.UserName = this.selectVote.IsRealName ? this.entityData.UserName : '';
                this.editEntityData = entityData;
                return entityData;
            },
            saveRecordCallback({ props }) {
                this.alertSuccess('提交成功', () => {
                    if (this.selectVote.IsChange === 0) {
                        this.selectVote.disabled = true;
                        props.property.setIsVisible(false);
                    }
                    this.selectVote.SelectItems = this.editEntityData.VoteItems;
                });
            }
        }
    });
})(window.aopc);